package com.edu.neusoft.cookbook.controller;


import com.edu.neusoft.cookbook.common.ResponseResult;
import com.edu.neusoft.cookbook.common.enums.AppHttpCodeEnum;
import com.edu.neusoft.cookbook.dto.RecipesDTO;
import com.edu.neusoft.cookbook.dto.UserDto;
import com.edu.neusoft.cookbook.entity.Recipes;
import com.edu.neusoft.cookbook.entity.User;
import com.edu.neusoft.cookbook.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * ClassName: UserController
 * Package: com.atyukaijun.flowers.controller
 * Description:
 *
 * @Author 麻了跺跺脚
 * @Create 2024/12/3 9:39
 * @Version 1.0
 */
@RestController
@Slf4j
@Api(value = "app端用户登录",tags = "app端用户登录")
@RequestMapping("/api/user")
public class UserController {

    @Autowired
    private UserService userService;

    /**
     * 登录接口
     * @param userDto
     * @return
     */
    @ApiOperation("登录")
    @PostMapping("/login")
    public ResponseResult login(@RequestBody UserDto userDto, HttpSession session){
        return userService.login(userDto, session);
    }

    /**
     * 登出接口
     * @param session
     * @return
     */
    @ApiOperation("登出")
    @PostMapping("/logout")
    public ResponseResult loginOut(HttpSession session){
        session.removeAttribute("loginUser");
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 展示个人信息接口
     * @param session
     * @return
     */
    @ApiOperation("展示个人信息")
    @GetMapping("show")
    public ResponseResult show(HttpSession session) {
        User user = (User) session.getAttribute("loginUser");
        log.info(String.valueOf(user.getId()));
        return ResponseResult.okResult(userService.getById(user.getId()));
    }

    /**
     *
     * @param userId
     * @return
     */
    @ApiOperation("查找用户收藏的菜谱")
    @GetMapping("/{userId}/favourites/recipes")
    public ResponseEntity<List<Recipes>> getFavouriteRecipes(@PathVariable Integer userId) {
        List<Recipes> recipes = userService.getFavouriteRecipesByUserId(userId);
        return ResponseEntity.ok(recipes);
    }


}
